/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.content {
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.dbg.model.content.MLDBGValuesListModel;

	import flash.utils.describeType;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGClassMethodsListModel extends MLDBGValuesListModel {

		private var obj:Object;
		private var methodsStringRefences:Array;

		public function MLDBGClassMethodsListModel(obj:Object) {
			super(false);
			this.obj=obj;
			refresh();
			refresh();
		}
		
		override public function refresh():void {
			clear();
			
			var typeXML:XML=describeType(obj);
			var i:uint,j:uint;
			var methodText:String, paramXML:XML, params:Array, className:String;
			methodsStringRefences=[];
						var classesData:Array=[];
			for(i=0;i<typeXML.method.length();i++){
				className=MLDBG.getClassNameByQualifiedClassName(typeXML.method[i].@declaredBy.toString());
				if(!methodsStringRefences[className]){
					methodsStringRefences[className]={
						name:MLDBG.formatClassName(className),
						methods:[]
					};
					classesData.push(methodsStringRefences[className]);
				}
				
				methodText=MLDBG.formatMethodName(typeXML.method[i].@name.toString())+'(';
				params=[];
				for(j=0;j<typeXML.method[i].parameter.length();j++){
					paramXML=typeXML.method[i].parameter[j];
					params.push(MLDBG.getClassNameByQualifiedClassName(paramXML.@type.toString()));
				}
				methodText+=params.join(', ')+')';
				methodText+=':'+MLDBG.getClassNameByQualifiedClassName(typeXML.method[i].@returnType.toString());
				if(!typeXML.method[i].parameter.length()){
					methodsStringRefences.push(obj[typeXML.method[i].@name.toString()]);
					methodText+='  '+MLDBG.getInstance().getValueLinked(obj[typeXML.method[i].@name.toString()], '<b>call</b>');
				}
				
				methodsStringRefences[className].methods.push(methodText);
				//addValue(methodText);
			}
			var loop:Object;
			//classesData.reverse();
			for(i=0;i<classesData.length;i++){
				loop=classesData[i];
				addValue(loop.name);
				///loop.methods.sort();
				for(j=0;j<loop.methods.length;j++){
					addValue('   '+loop.methods[j]);
				}
			}
		}
		
	}
}
